package com.summer.shopping.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.shopping.domain.common.req.ReqHolder;
import com.summer.shopping.domain.table.Product;
import com.summer.shopping.domain.table.ShoppingCart;
import com.summer.shopping.domain.table.dto.PageDTO;
import com.summer.shopping.domain.table.dto.ShoppingCarDTO;
import com.summer.shopping.domain.table.vo.ShoppingCartVo;
import com.summer.shopping.mapper.ProductMapper;
import com.summer.shopping.mapper.ShoppingCartMapper;
import com.summer.shopping.service.ShoppingCartService;
import com.summer.shopping.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart>
    implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    ProductMapper productMapper;
    @Override
    public void add(ShoppingCarDTO shoppingCartDTO) {
        Long userId = ReqHolder.getUserId();
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);

        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        queryWrapper.eq(ShoppingCart::getProductId,shoppingCart.getProductId());
        List<ShoppingCart> list = shoppingCartMapper.selectList(queryWrapper);
        if (list!=null && list.size()>0){
            Integer number = list.get(0).getNumber();
            ShoppingCart updateShoppingCart = list.get(0);
            updateShoppingCart.setNumber(number+1);
            shoppingCartMapper.updateById(updateShoppingCart);
        }else {

            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    @Override
    public void clean() {
        Long userId = ReqHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        shoppingCartMapper.delete(queryWrapper);

    }

    @Override
    public void delete(List<Long> ids) {
        shoppingCartMapper.deleteBatchIds(ids);
    }

    @Override
    public PageResult<ShoppingCartVo> getPageList(PageDTO pageDTO) {
        Long userId = ReqHolder.getUserId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);

        IPage<ShoppingCart> page=new Page<>(pageDTO.getPageNo(),pageDTO.getPageSize());
        IPage<ShoppingCart> shoppingCartIPage = shoppingCartMapper.selectPage(page, queryWrapper);
        List<ShoppingCart> shoppingCarts = shoppingCartIPage.getRecords();

//        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);

        List<Long> pIds = shoppingCarts.stream().map(ShoppingCart::getProductId).collect(Collectors.toList());
        List<ShoppingCartVo> list=new ArrayList<>();
        if (pIds != null && pIds.size()>0){
            List<Product> products = productMapper.selectBatchIds(pIds);
            for (ShoppingCart shoppingCart : shoppingCarts) {
                ShoppingCartVo vo = new ShoppingCartVo();
                BeanUtils.copyProperties(shoppingCart,vo);
                Product p=getPByPidTPL(vo.getProductId(),products);
                vo.setName(p.getName());
                vo.setImage(p.getImg());
                vo.setAmount(p.getPrice());
                list.add(vo);
            }
        }

        PageResult<ShoppingCartVo> shoppingCartVoPageResult=new PageResult<>();
        shoppingCartVoPageResult.setRecords(list);
        shoppingCartVoPageResult.setTotal(shoppingCartIPage.getTotal());
        return shoppingCartVoPageResult;
    }

    private Product getPByPidTPL(long productId,List<Product> list) {
        Product p = new Product();
        for (Product product : list) {
            if (product.getId() == productId){
                p=product;
                break;
            }
        }
        return p;
    }

}
